package com.situ.mall.controller.front;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.mall.entity.Product;
import com.situ.mall.service.IProductService;

@Controller
@RequestMapping("/product")
public class FrontProductController {
	
	@Autowired
	private IProductService productService;

//测试
	@RequestMapping("/selectAll.shtml")
	@ResponseBody
	public List<Product> selectAll() {
		System.out.println("FrontProductController.selectAll()");
		return productService.selectAll();
	}
	
// 前台
	
	// 1.product_list.jsp 拿到 商品集合 (在index.jsp 点击二级分类后 [点击手机]) 
	@RequestMapping("/getProductList.shtml")
	public String getProductList(Integer secondCategoryId, Model model) {
		System.out.println("FrontProductController.getProductList()");
		System.out.println("secondCategoryId："+secondCategoryId);
		
		//1. 取出 商品集合 by category_id(二级分类的id)
		//2.放进作用域，转发到 商品集合 页面
		List<Product> list = productService.selectProductListBySecondCategoryId(secondCategoryId);
		model.addAttribute("secondProductList",list);
		return "product_list";
	}
	
	// 2. product_list.jsp --->  拿到 商品详情(product), 根据 商品id [product.id] ---> product_detail.jsp 
	@RequestMapping("/getProductDetailJsp.shtml")
	public String getProductDetailJsp(Integer productId, Model model) {
		System.out.println("FrontProductController.getProductDetailJsp()");
		System.out.println("productId商品Id:："+productId);
		
		Product product = productService.selectProductByProductId(productId);
		model.addAttribute("product",product);
		
		return "product_detail";
	}
}
